package contactos;

import java.util.Enumeration;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.pim.Contact;
import javax.microedition.pim.ContactList;
import javax.microedition.pim.PIM;

public class PIMMIDlet extends MIDlet implements CommandListener {
  private ContactList contList = null;

  private Enumeration contacts = null;

  private List mNameList;

  private Command mExitCommand= new Command("Exit", Command.EXIT, 0);
  private Command nuevoContacto= new Command("Nuevo", Command.EXIT, 0);
  private Command modificarContacto= new Command("Modificar", Command.EXIT, 0);

  public PIMMIDlet() {
    try {
      PIM pimInst = PIM.getInstance();
      contList = (ContactList) pimInst.openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY);
      contacts = contList.items();
      mNameList = new List("List of contacts", List.EXCLUSIVE);
    } catch (Exception ex) {
      return;
    }

   
    if (contacts == null)
        return;
      mNameList = new List("List of contacts", List.EXCLUSIVE);
      while (contacts.hasMoreElements()) {
        Contact tCont = (Contact) contacts.nextElement();
        String[] nameValues = tCont.getStringArray(Contact.NAME, 0);
        String firstName = nameValues[Contact.NAME_GIVEN];
        String lastName = nameValues[Contact.NAME_FAMILY];
        mNameList.append(lastName + ", " + firstName, null);
      }
      mNameList.addCommand(mExitCommand);
      mNameList.setCommandListener(this);

  }

  public void startApp() {
    Display.getDisplay(this).setCurrent(mNameList);
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean flg) {
  }

  public void commandAction(Command c, Displayable s) {
    if (c == mExitCommand) {
      destroyApp(true);
      notifyDestroyed();
    }
  }
}

